package com.koron.css2.clientCenter.impl;


import com.google.common.collect.Maps;
import com.koron.css2.ServerInterface;
import com.koron.css2.clientCenter.bean.UserBean;
import com.koron.css2.clientCenter.bean.UserChangeItemBean;
import com.koron.css2.clientCenter.bean.ValveCloseDetailBean;
import com.koron.css2.clientCenter.mapper.ChangeUserMapper;
import com.koron.css2.clientCenter.mapper.UserInfoMapper;
import com.koron.css2.clientCenter.mapper.ValveCloseDetailMapper;
import com.koron.css2.clientCenter.mapper.ValveUserMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.css2.serviceManage.utils.JsonUtils;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.util.CollectionUtils;
import org.swan.bean.MessageBean;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 取消关阀
 *
 * @author cd
 * @author modify by 杨嘉慧
 */
public class ValveCloseCancel implements ServerInterface {

    @Override
    public MessageBean<?> exec(SessionFactory factory, UserInfoBean userInfo, RequestBean req) {

        try {
            ValveCloseDetailBean detailBean = JsonUtils.objectToPojo(req.getData(), ValveCloseDetailBean.class);
            ValveCloseDetailMapper detailMapper = factory.getMapper(ValveCloseDetailMapper.class);

            //记录阀门状态前的用户信息
            UserInfoMapper userMapper = factory.getMapper(UserInfoMapper.class);
            UserBean beforeUser = userMapper.getUserInfoByUserNo(detailBean.getUserNo(), 1);
            ValveUserMapper valveUserMapper2 = factory.getMapper(ValveUserMapper.class);
            String valveStatus = valveUserMapper2.getValveStatus(detailBean.getUserNo());
            beforeUser.setValveStatus(Tools.getDicNameByCodeAndValue(factory,"MVS",valveStatus));


            //修改关阀单明细数据阀门状态为已取消
            detailMapper.updateCancelClose(detailBean.getId(), userInfo.getUserInfo().getAcount(), userInfo.getUserInfo().getName());
            ValveUserMapper valveUserMapper = factory.getMapper(ValveUserMapper.class);
            //将用户、水表表阀门状态改为阀开
            valveUserMapper.openValve(detailBean.getUserNo());

            //用户变更事项记录
            List<UserChangeItemBean> item = new ArrayList<UserChangeItemBean>();
            item.addAll(userChangeItems(factory,detailBean,userInfo, beforeUser));
            //添加用户变更事项记录
            ChangeUserMapper changeUserMapper = factory.getMapper(ChangeUserMapper.class);
            if (!CollectionUtils.isEmpty(item)) {
                changeUserMapper.userChangeItemAdd(item, 1);
            }
            return MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "取消关阀成功", null);
        } catch (Exception e) {
            logger.error("取消关阀失败", e);
            return MessageBean.create(Constant.MESSAGE_INT_FAIL, "取消关阀失败", null);
        }
    }


    /**
     * 用户变更事项
     *
     * @param factory
     * @param userInfo
     * @return
     * @throws Exception
     */
    private List<UserChangeItemBean> userChangeItems(SessionFactory factory, ValveCloseDetailBean detailBean, UserInfoBean userInfo, UserBean beforeUserInfo) throws Exception {
        List<UserChangeItemBean> item = new ArrayList<UserChangeItemBean>();
        UserBean afterUserInfo = beforeUserInfo.clone();
        //获取客户、用户字段中英文
        Map<String, String> userFieldTemp = Tools.getFiledName(factory, "user_info");
        Map<String, String> userField = Maps.newHashMap();
        Map<String, String> changeNos = new HashMap<String, String>();
        changeNos.put("userNo", detailBean.getUserNo());
        //changeNos.put("receiptId", bean.getId());
        changeNos.put("receiptType", "VALVE_MANAGEMENT");

        UserBean userBean = new UserBean();
        userBean.setUserNo(detailBean.getUserNo());
        ValveUserMapper valveUserMapper = factory.getMapper(ValveUserMapper.class);
        String valveStatus = valveUserMapper.getValveStatus(detailBean.getUserNo());
        userBean.setValveStatus(Tools.getDicNameByCodeAndValue(factory,"MVS",valveStatus));


        for (String field : userFieldTemp.keySet()) {
            try {
                Field f = UserBean.class.getDeclaredField(field);
                f.setAccessible(true);
                if (f.get(userBean) != null) {
                    userField.put(field, userFieldTemp.get(field));
                    f.set(afterUserInfo, f.get(userBean));
                }
            } catch (NoSuchFieldException e) {
                continue;
            }
        }
        List<UserChangeItemBean> userItem = Tools.getChangeItems(userInfo, changeNos, beforeUserInfo, afterUserInfo, userField);
        item.addAll(userItem);
        return item;
    }
}

